package com.round.card

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.animation.Animation
import android.view.animation.Animation.AnimationListener
import android.widget.FrameLayout
import android.widget.RelativeLayout

/**
 * Author: Wu Chang
 *
 *
 * Date: 2024/12/5 14:24
 */
class TurnRoundCardView @JvmOverloads constructor(
    context: Context?,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : RelativeLayout(context, attrs, defStyleAttr) {

    companion object{
        private const val ANGLE_0 = 0f
        private const val ANGLE_90 = 90f
        private const val ANGLE_180 = 180f
        private const val ANGLE_270 = 270f
        private const val ANGLE_360 = 360f

        const val FLIP_COMPLETE = 0
        const val FLIP_START = 1
    }

    private var flipStateMLD:(Int)-> Unit = {}

    var cardFront: FrameLayout
        private set

    var cardBack: FrameLayout
        private set

    var frontBody: View?= null
        private set
    var backBody: View?= null
        private set

    var duration = 1000L

    var repeatCount = 0

    private var isAnimation = false

    private var rotation = 0f

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.view_trun_card, this, true)
        cardFront = findViewById(R.id.frame_front)
        cardBack = findViewById(R.id.frame_back)
    }

    fun addFront(front: View) {
        frontBody = front
        cardFront.addView(front, FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT))
    }

    fun addBack(back: View) {
        backBody = back
        cardBack.addView(back, FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT))
    }

    fun clearFront() {
        cardFront.removeAllViews()
        frontBody = null
    }

    fun clearBack() {
        cardBack.removeAllViews()
        backBody = null
    }

    fun OnFlipState(state:(Int) -> Unit) {
        flipStateMLD = state
    }

    fun turn() {
        if (isAnimation) return
        if (rotation >= ANGLE_360) rotation = ANGLE_0
        val start = rotation
        val animator = TurnAnimation(start, {
            rotation = start + it
            if (rotation >= ANGLE_90 && rotation < ANGLE_180) {
                cardBack.visibility = VISIBLE
                cardFront.visibility = INVISIBLE
            } else if (rotation >= ANGLE_270 && rotation < ANGLE_360) {
                cardBack.visibility = INVISIBLE
                cardFront.visibility = VISIBLE
            }
            if (rotation >= start + ANGLE_180) {
                isAnimation = false
            }
        }, duration)

        animator.duration = duration
        animator.repeatCount = repeatCount
        animator.interpolator = AccelerateDecelerateInterpolator()
        animator.setAnimationListener(object : AnimationListener {
            override fun onAnimationStart(animation: Animation?) {
                isAnimation = true
                flipStateMLD.invoke(FLIP_START)
            }

            override fun onAnimationEnd(animation: Animation?) {
                flipStateMLD.invoke(FLIP_COMPLETE)
            }

            override fun onAnimationRepeat(animation: Animation?) {
            }

        })
        animator.fillAfter = true
        this.startAnimation(animator)
    }
}
